package com.zhanghuang.config;

import com.alibaba.fastjson.JSON;
import com.zhanghuang.format.FormatProcessor;
import com.zhanghuang.format.JsonFormatProcessor;
import com.zhanghuang.format.StringFormatProcessor;
import com.zhanghuang.template.HelloFormatTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


// 装配进来配置类
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {


    @Bean
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
    public FormatProcessor stringFormat(){
        return new StringFormatProcessor();
    }

    @Bean
    @Primary
    @ConditionalOnClass(name="com.alibaba.fastjson.JSON")
    public FormatProcessor jsonFormat(){
        return new JsonFormatProcessor();
    }

    @Bean
    public HelloFormatTemplate getHelloTemplate(FormatProcessor f){
        return new HelloFormatTemplate(f);
    }


}
